<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="650px" @confirm="handleSubmit" @close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="80px" :rules="formRules">
				<el-form-item label="名称" prop="name">
					<el-input v-model="formData.name" placeholder="请输入服务商名称" clearable />
				</el-form-item>
				<el-form-item label="身份证号" prop="id_card">
					<el-input v-model="formData.id_card" placeholder="请输入身份证号" clearable />
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="formData.phone" placeholder="请输入手机号" clearable />
				</el-form-item>
				<el-form-item label="服务年限" prop="serve_year">
					<el-input v-model="formData.serve_year" placeholder="请输入服务年限" clearable>
						<template #append>年</template>
					</el-input>
				</el-form-item>
				<el-form-item label="简介" prop="desc">
					<el-input v-model="formData.desc" type="textarea" :rows="2" placeholder="请输入简介" clearable />
				</el-form-item>
				<el-form-item label="标签" prop="tags">
					<div v-for="(item,index) in formData.tags" :key="'tag'+index" style="width:100%; display:flex; align-items: center; margin-bottom: 10px;">
						<el-input v-model="item.name" size="small" placeholder="请输入" clearable />
						<el-button type="danger" plain size="small" @click="deleteTag(index)" style="margin-left: 5px;">删除</el-button>
					</div>
					<el-button type="primary" size="small" @click="addTag">新增</el-button>
				</el-form-item>
				<el-form-item label="擅长" prop="good_skill">
					<el-input v-model="formData.good_skill" placeholder="请输入擅长" clearable />
				</el-form-item>
				<el-form-item label="职位" prop="position_name">
					<el-input v-model="formData.position_name" placeholder="请输入职位" clearable />
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="formData.address" placeholder="请输入地址" clearable />
				</el-form-item>
				<el-form-item label="分类" prop="cate_id">
					<el-select v-model="formData.cate_id">
						<el-option v-for="(item) in optionsData.cate_list.lists" :label="item.name" :value="item.id" :key="item.id" />
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="icon" prop="icon">
					<div class="w-80">
						<div>
							<material-picker v-model="formData.icon" :limit="1" />
						</div>
						<div class="form-tips">支持扩展名：.jpg .jpeg .png .bmp</div>
					</div>
				</el-form-item> -->
				<!-- <el-form-item label="类型" prop="type">
					<el-radio-group v-model="formData.type">
						<el-radio :label="1">小程序</el-radio>
						<el-radio :label="2">超链接</el-radio>
					</el-radio-group>
				</el-form-item> -->
				<!-- <el-form-item label="启用状态" prop="is_enable" required>
					<el-radio-group v-model="formData.is_enable">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item> -->
			</el-form>
		</popup>
	</div>
</template>
<script lang="ts" setup>
	import type { FormInstance } from 'element-plus'
	import { cateList, saveUser } from '@/api/service';
	import Popup from '@/components/popup/index.vue'
	import { getModulesKey } from '@/router'
	import { useDictOptions } from "@/hooks/useDictOptions";
import { shallowRef, ref, computed, reactive } from 'vue';
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑服务商' : '新增服务商'
	})

	const componentsOptions = ref(getModulesKey())
	const querySearch = (queryString : string, cb : any) => {
		const results = queryString
			? componentsOptions.value.filter((item) =>
				item.toLowerCase().includes(queryString.toLowerCase())
			)
			: componentsOptions.value
		cb(results.map((item) => ({ value: item })))
	}
	
	const { optionsData } = useDictOptions<{
		cate_list : any[],
	}>({
		cate_list: {
			api: cateList
		}
	})
	
	const formData = reactive({
		id: '',
		name: '',
		id_card: '',
		phone: '',
		serve_year: '',
		desc: '',
		tags: [],
		good_skill: '',
		position_name: '',
		address: '',
		cate_id: '',
		// icon: '',
		// type: 1,
		// is_enable: 1
	})
	
	const formRules = {
		name: [
			{
				required: true,
				message: '请输入名称',
				trigger: 'blur'
			}
		],
		id_card: [
			{
				required: true,
				message: '请输入身份证号',
				trigger: 'blur'
			}
		],
		phone: [
			{
				required: true,
				message: '请输入手机号',
				trigger: 'blur'
			}
		],
		serve_year: [
			{
				required: true,
				message: '请输入服务年限',
				trigger: 'blur'
			}
		],
		// icon: [
		// 	{
		// 		required: true,
		// 		message: '请上传icon',
		// 		trigger: 'blur'
		// 	}
		// ],
		type: [
			{
				required: true,
				message: '请选择类型',
				trigger: 'blur'
			}
		],
		link: [
			{
				required: true,
				message: '请输入链接',
				trigger: 'blur'
			}
		],
		app_id: [
			{
				required: true,
				message: '请输入appid',
				trigger: 'blur'
			}
		]
	}
	
	const deleteTag = async (index: any) => {
		formData.tags.splice(index,1);
	}
	
	const addTag = async (index: any) => {
		formData.tags.push({name:''});
	}
	
	const menuOptions = ref<any[]>([])

	const handleSubmit = async () => {
		await formRef.value?.validate()
		let newFormData = JSON.parse(JSON.stringify(formData));
		newFormData.tags = formData.tags.map((item)=>{
			return item.name;
		}).join(',');
		mode.value == 'edit' ? await saveUser(newFormData) : await saveUser(newFormData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	const setFormData = (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
				if( key=='tags' ){
					formData[key] = [];
					data[key].forEach((item)=>{
						formData[key].push({name:item});
					});
				}
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		setFormData(row)
	}

	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style scoped lang="scss"></style>